package net.hky365.lowcode.magic.interceptor;

import net.hky365.shop.framework.common.pojo.PageableResult;
import net.hky365.shop.framework.common.util.CommonUtils;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.context.RequestEntity;
import org.ssssssss.magicapi.core.interceptor.DefaultResultProvider;
import org.ssssssss.magicapi.core.interceptor.ResultProvider;
import org.ssssssss.magicapi.modules.db.model.Page;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 自定义magic 返回内容
 *
 * @author hl-king
 */
@Component
public class MagicResultProvider extends DefaultResultProvider implements ResultProvider {

    public MagicResultProvider(MagicAPIProperties properties) {
        super(properties.getResponse());
    }

    @Override
    public Object buildPageResult(RequestEntity requestEntity, Page page, long total, List<Map<String, Object>> data) {
        // 自定义分页数据返回
        return PageableResult.of(((Double) Math.ceil(page.getOffset() / (double) page.getLimit())).longValue() + 1,
                page.getLimit(),
                total,
                CommonUtils.nonNullOrDefault(data, Collections.emptyList()));
    }
}